<style>
.pure_form_extra label, .pure_form_extra input { font-size: 12px !important; }
.pure_form_extra * { font-size: 12px !important; }
.pure_form_extra .mandatory { color: #ff0000; padding-left: 5px; }
</style>
<?php 
$_SESSION[TINYMCE_UPLOAD] = 'upload/plugins/' . basename(dirname(__FILE__)) . '/';
if (!file_exists('../' . $_SESSION[TINYMCE_UPLOAD])) {
  Utils::create('../' . $_SESSION[TINYMCE_UPLOAD]);
}

$id = Utils::get_param('id', 0);
$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_ht_customer', NULL, NULL, NULL, NULL, $opts)->d;

$pg = Utils::get_param('pg', 1);
$objs = $orm->get('pb_ht_customer', 't.account', 'ASC', $pg, SIZE, NULL);
?>
<div id="register" class="pure-form pure-form-aligned pure_form_extra" style="width: 950px;">
  <div class="pure-control-group">
    <label>邮箱<span class="mandatory">*</span></label>
    <input id="reg_user" type="text" class="pure-input-1-2" placeholder="{email}" readonly="true"/>
  </div>
  
  <div class="pure-control-group">
    <label>密码</label>
    <input id="reg_pass" type="password" class="pure-input-1-2" placeholder="{password}" />
  </div>
  
  <div class="pure-control-group">
    <label>姓名<span class="mandatory">*</span></label>
    <input id="reg_last" type="text" placeholder="{family name}"/>
    <input id="reg_first" type="text" placeholder="{given name}"/>
  </div>
  
  <div class="pure-control-group">
    <label>日常居住地<span class="mandatory">*</span></label>
    <select id="living_country" class="pure-input-1-2" onchange="render();">
      <?php foreach ($LIVING_COUNTRIES as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>&nbsp;</label>
    <select id="living_area" class="pure-input-1-2"></select>
  </div>
  
  <div class="pure-control-group">
    <label>家庭收入<span class="mandatory">*</span></label>
    <select id="family_income" class="pure-input-1-2">
      <?php foreach ($FAMILY_INCOMES as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>准备购买<span class="mandatory">*</span></label>
    <select id="investment_type" class="pure-input-1-2">
      <?php foreach ($INVEST_TYPES as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>目标区域<span class="mandatory">*</span></label>
    <select id="interested_area" class="pure-input-1-2">
      <?php foreach ($NZ_AREAS as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>特别中意街道/地区</label>
    <input id="interested_area_de" type="text" class="pure-input-1-2" placeholder="{interested area}"/>
  </div>
  
  <div class="pure-control-group">
    <label>房型<span class="mandatory">*</span></label>
    <select id="property_type" class="pure-input-1-2">
      <?php foreach ($HOUSE_TYPES as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>卧室数量<span class="mandatory">*</span></label>
    <select id="bedroom_num" class="pure-input-1-2">
      <?php foreach ($BDRM_NUM as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>价格区间<span class="mandatory">*</span></label>
    <select id="price_range" class="pure-input-1-2">
      <?php foreach ($PRICE_RANGE as $itm) { ?>
      <option value="<?php echo $itm; ?>"><?php echo $itm; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>订阅邮件<span class="mandatory">*</span></label>
    <select id="email_required" class="pure-input-1-2">
      <option value="<?php echo 1; ?>">是</option>
      <option value="<?php echo 0; ?>">否</option>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>&nbsp;</label>
    <button class="pure-button pure-input-1-2 pure-button-primary" onclick="submit_obj();">确认</button>
  </div>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __objs = JSON.parse('<?php echo addslashes(json_encode($objs)); ?>');
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __cities = JSON.parse('<?php echo addslashes(json_encode($CN_CITIES)); ?>');
  var __areas = JSON.parse('<?php echo addslashes(json_encode($NZ_AREAS)); ?>');
  
  $(function(){
    
    render();
  });

  function render() {
    Utils.leftbar(
      __objs,
      __data[0].id,
      '会员',
      'account',
      function(page) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=' + __file + '&id=' + __data[0].id + '&pg=' + page);
      }
    );
    render_data();
  };

  function render_data() {
    Url.insert_param('id', __data[0].id);

    $('#reg_user').val(__data[0].account);
    $('#reg_last').val(__data[0].last_name);
    $('#reg_first').val(__data[0].first_name);
    
    $('#living_country').val(__data[0].living_country);
    $('#living_area').empty();
    if ($('#living_country :selected').text() == '中国') {
      arr = __cities;
    } else {
      arr = __areas;
    }
    for (var idx in arr) {
      var itm = arr[idx];
      $('#living_area').append('<option' + ((__data[0].living_area == itm) ? ' selected="selected"' : '') + '>' + itm + '</option>');
    }
    $('#family_income').val(__data[0].family_income);

    $('#investment_type').val(__data[0].investment_type);
    $('#interested_area').val(__data[0].interested_area);
    $('#interested_area_de').val(__data[0].interested_area_de);
    $('#property_type').val(__data[0].property_type);
    $('#bedroom_num').val(__data[0].bedroom_num);
    $('#price_range').val(__data[0].price_range);
    $('#email_required').val(__data[0].email_required);
  };

  function submit_obj() {
    __data[0].__password = $('#reg_pass').val();
    __data[0].first_name = $('#reg_first').val();
    __data[0].last_name = $('#reg_last').val();
    __data[0].living_country = $('#living_country :selected').text();
    __data[0].living_area = $('#living_area :selected').text();
    __data[0].family_income = $('#family_income :selected').text();
    __data[0].investment_type = $('#investment_type :selected').text();
    __data[0].interested_area = $('#interested_area :selected').text();
    __data[0].interested_area_de = $('#interested_area_de').val();
    __data[0].property_type = $('#property_type :selected').text();
    __data[0].bedroom_num = $('#bedroom_num :selected').text();
    __data[0].price_range = $('#price_range :selected').text();
    __data[0].email_required = $('#email_required :selected').text() == '是' ? 1 : 0;
    __data[0].__pg = __objs.p;

    Ajax.request(
      __requrl.replace(__file, 'index') + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(__data[0])),
      function(rslt) {
        if (rslt.__objs) {
          __objs = rslt.__objs;
          delete rslt.__objs;
        }
        __data[0] = rslt;
        render();
      },
      null
    );
  };
  
  function get_obj(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'get_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>